package com.meihuayishu.backend.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * Spring AI 配置类
 * 使用Spring AI 1.0.0-M4官方标准配置方式，支持多AI提供商
 */
@Configuration
@Slf4j
public class SpringAiConfig {

    /**
     * 默认ChatClient - 根据配置自动选择AI提供商
     */
    @Bean
    @Primary
    public ChatClient defaultChatClient(ChatClient.Builder chatClientBuilder) {
        log.info("配置默认Spring AI ChatClient");
        return chatClientBuilder.build();
    }
    
    /**
     * 智谱AI ChatClient
     */
    @Bean("zhipuChatClient")
    @ConditionalOnProperty(name = "spring.ai.zhipu.api-key")
    public ChatClient zhipuChatClient(
            @Value("${spring.ai.zhipu.api-key}") String apiKey,
            @Value("${spring.ai.zhipu.base-url}") String baseUrl,
            @Value("${spring.ai.zhipu.chat.options.model:glm-4-plus}") String model,
            @Value("${spring.ai.zhipu.chat.options.temperature:0.7}") Double temperature,
            @Value("${spring.ai.zhipu.chat.options.max-tokens:2000}") Integer maxTokens) {
        
        log.info("配置智谱AI ChatClient: model={}, baseUrl={}", model, baseUrl);
        
        var zhipuApi = new OpenAiApi(baseUrl, apiKey);
        var zhipuChatModel = new OpenAiChatModel(zhipuApi, 
            OpenAiChatOptions.builder()
                .withModel(model)
                .withTemperature(temperature)
                .withMaxTokens(maxTokens)
                .build());
        
        return ChatClient.builder(zhipuChatModel).build();
    }
    
    /**
     * DeepSeek ChatClient
     */
    @Bean("deepseekChatClient")
    @ConditionalOnProperty(name = "spring.ai.deepseek.api-key")
    public ChatClient deepseekChatClient(
            @Value("${spring.ai.deepseek.api-key}") String apiKey,
            @Value("${spring.ai.deepseek.base-url}") String baseUrl,
            @Value("${spring.ai.deepseek.chat.options.model:deepseek-chat}") String model,
            @Value("${spring.ai.deepseek.chat.options.temperature:0.7}") Double temperature,
            @Value("${spring.ai.deepseek.chat.options.max-tokens:2000}") Integer maxTokens) {
        
        log.info("配置DeepSeek ChatClient: model={}, baseUrl={}", model, baseUrl);
        
        var deepseekApi = new OpenAiApi(baseUrl, apiKey);
        var deepseekChatModel = new OpenAiChatModel(deepseekApi,
            OpenAiChatOptions.builder()
                .withModel(model)
                .withTemperature(temperature)
                .withMaxTokens(maxTokens)
                .build());
        
        return ChatClient.builder(deepseekChatModel).build();
    }
    
    /**
     * OpenAI ChatClient (使用默认配置)
     */
    @Bean("openaiChatClient")
    @ConditionalOnProperty(name = "spring.ai.openai.api-key")
    public ChatClient openaiChatClient(ChatClient.Builder chatClientBuilder) {
        log.info("配置OpenAI ChatClient");
        return chatClientBuilder.build();
    }
}